package cn.flkj.lucia2a2.controllers;

import cn.flkj.lucia2a2.config.EnvRunner;
import cn.flkj.lucia2a2.config.Org2Runner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author : 州长在手 2021/1/7 下午5:02
 */

@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/org2")
public class Org2Controller {
    private final Org2Runner org2Runner;

    private final String  CHAINCODE_NAME = "fabcar";
    public Org2Controller(Org2Runner org2Runner) {
        this.org2Runner = org2Runner;
    }

    @PostMapping("/install")
    public Object install(
            @RequestParam("chaincodeName") String chaincodeName,
            @RequestParam("chaincodeVersion") String chaincodeVersion,
            @RequestParam("chaincodeLabel") String chaincodeLabel,
            @RequestParam("chaincodeSourceLocation") String chaincodeSourceLocation,
            @RequestParam("chaincodePath") String chaincodePath,
            @RequestParam("metadadataSource") String metadadataSource,
            @RequestParam("chaincodeEndorsementPolicyPath") String chaincodeEndorsementPolicyPath) {
        try {
            return     org2Runner.getFm().lifecycleChaincodeInstall(chaincodeName,chaincodeVersion,chaincodeLabel,chaincodeSourceLocation,chaincodePath,metadadataSource,chaincodeEndorsementPolicyPath);

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return "lucia !";
    }


    @PostMapping("/approveformyorg")
    public Object approveformyorg(  @RequestParam("chaincodeName") String chaincodeName,
                                    @RequestParam("chaincodeVersion") String chaincodeVersion,
                                    @RequestParam("packageid") String packageid){
        try {
            return org2Runner.getFm().lifecycleChaincodeApproveformyorg(chaincodeName,chaincodeVersion,packageid);

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return "lucia !";

    }
}
